Skip to content

fix(vite): handle dotted Nitro routes under baseURL in dev#4108

Merged
pi0 merged 5 commits intomainfrom
fix-vite-dotted
Mar 23, 2026
Merged

fix(vite): handle dotted Nitro routes under baseURL in dev#4108
pi0 merged 5 commits intomainfrom
fix-vite-dotted

Conversation

@schiller-manuel
Copy link
Contributor

@schiller-manuel schiller-manuel requested a review from pi0 as a code owner March 13, 2026 18:23
@vercel
Copy link

vercel bot commented Mar 13, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
nitro.build Ready Ready Preview, Comment Mar 23, 2026 11:00pm

Request Review

@coderabbitai
Copy link

coderabbitai bot commented Mar 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 340a4d2d-6d98-4c01-bca0-8d957a6a48ea

📥 Commits

Reviewing files that changed from the base of the PR and between f761fcf and 8854f47.

📒 Files selected for processing (1)
  • test/vite/baseurl-dotted-param.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/vite/baseurl-dotted-param.test.ts

📝 Walkthrough

Walkthrough

Normalizes incoming dev request URLs with the configured baseURL and detects Nitro routes (including splat routes with dotted params) before treating requests as static assets; restores the original request URL after handling. Adds a fixture and integration test for dotted splat params under a subdirectory base.

Changes

Cohort / File(s) Summary
Vite dev middleware
src/build/vite/dev.ts
Wraps nodeReq.url with withBase(nodeReq.url, baseURL) for normalization, computes file extension, uses nitro.routing.routes.match (with withBase) to detect Nitro routes (setting an isNitroRoute exception), and restores the original request URL in a finally block.
Dotted-param test fixture
test/vite/baseurl-dotted-param-fixture/api/proxy/[...param].ts, test/vite/baseurl-dotted-param-fixture/index.html, test/vite/baseurl-dotted-param-fixture/tsconfig.json, test/vite/baseurl-dotted-param-fixture/vite.config.ts
Adds a Vite/Nitro fixture serving under base: "/subdir/" with a catch-all API route that returns the splat param and supporting config/files.
Integration test
test/vite/baseurl-dotted-param.test.ts
New Vitest that starts a Vite dev server pointing to the fixture and asserts requests to /subdir/api/proxy/todos/Package.todos.Entity.3 return 200 and the expected response body across request variants.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

  • #6903: Matches the infinite-redirect with TanStack Start + Nitro and basePath for dotted splat params; this change targets the same routing mismatch.
  • infinite redirect with baseUrl + nested tRPC procedure #3990: Addresses URL normalization and classification of dotted routes as Nitro routes rather than static assets, which is the same area this PR modifies.

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title follows conventional commits format with 'fix' scope and clearly describes the main change: handling dotted Nitro routes under baseURL in dev.
Description check ✅ Passed The description references the linked GitHub issue that documents the problem being fixed, relating directly to the changeset addressing infinite redirects with dotted URL parameters under baseURL.
Linked Issues check ✅ Passed The PR implements fixes for the infinite redirect issue (#6903) by adding base URL normalization in vite/dev.ts, introducing test fixtures, and creating comprehensive test coverage for dotted parameters under baseURL.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the baseURL and dotted route handling issue: vite dev middleware modifications, test fixtures, and test cases directly address the reported problem.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-vite-dotted

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 13, 2026

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4108

commit: 8854f47

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
test/vite/baseurl-dotted-param.test.ts (1)

15-29: Consider restoring the original working directory in afterAll.

process.chdir(rootDir) modifies global process state. While the test is marked sequential, restoring the cwd in the teardown would improve isolation and prevent potential side effects on subsequent test files.

♻️ Suggested improvement
+  let originalCwd: string;
+
   beforeAll(async () => {
+    originalCwd = process.cwd();
     process.chdir(rootDir);
     server = await createServer({ root: rootDir });
     await server.listen("0" as unknown as number);
     const addr = server.httpServer?.address() as {
       port: number;
       address: string;
       family: string;
     };
     serverURL = `http://${addr.family === "IPv6" ? `[${addr.address}]` : addr.address}:${addr.port}`;
   }, 30_000);

   afterAll(async () => {
     await server?.close();
+    if (originalCwd) {
+      process.chdir(originalCwd);
+    }
   });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/vite/baseurl-dotted-param.test.ts` around lines 15 - 29, The test
changes the process cwd in beforeAll via process.chdir(rootDir) but never
restores it; capture the original working directory (e.g., const originalCwd =
process.cwd()) before calling process.chdir in the beforeAll block and then
restore it in afterAll (call process.chdir(originalCwd)) alongside closing the
server in the existing afterAll to avoid leaking global state; update the
beforeAll/afterAll in this test file (the hooks named beforeAll and afterAll in
test/vite/baseurl-dotted-param.test.ts) accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@test/vite/baseurl-dotted-param.test.ts`:
- Around line 15-29: The test changes the process cwd in beforeAll via
process.chdir(rootDir) but never restores it; capture the original working
directory (e.g., const originalCwd = process.cwd()) before calling process.chdir
in the beforeAll block and then restore it in afterAll (call
process.chdir(originalCwd)) alongside closing the server in the existing
afterAll to avoid leaking global state; update the beforeAll/afterAll in this
test file (the hooks named beforeAll and afterAll in
test/vite/baseurl-dotted-param.test.ts) accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6033fe32-8363-4d31-9607-44c65e872049

📥 Commits

Reviewing files that changed from the base of the PR and between ba5147a and dcfe759.

📒 Files selected for processing (6)
  • src/build/vite/dev.ts
  • test/vite/baseurl-dotted-param-fixture/api/proxy/[...param].ts
  • test/vite/baseurl-dotted-param-fixture/index.html
  • test/vite/baseurl-dotted-param-fixture/tsconfig.json
  • test/vite/baseurl-dotted-param-fixture/vite.config.ts
  • test/vite/baseurl-dotted-param.test.ts

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/build/vite/dev.ts`:
- Around line 203-207: The dev server applies withBase(nodeReq.url, baseURL) too
early which lets unbased requests like /api/proxy/... match routes that should
require the base (e.g., /subdir/api/proxy/...), causing dev/production
divergence; update the pre-check in src/build/vite/dev.ts to use the raw request
pathname (nodeReq.url or req.url) when testing route matches (i.e., do not call
withBase(...) for the initial match) so unbased URLs are rejected when
nitro.options.baseURL is set, and add a regression test (e.g., extend
test/vite/baseurl-dotted-param.test.ts) asserting that an unbased URL fails or
behaves identically to production when baseURL="/subdir".
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bac1297a-d13a-4fe2-a9ce-452c0af4e2bf

📥 Commits

Reviewing files that changed from the base of the PR and between dcfe759 and f761fcf.

📒 Files selected for processing (1)
  • src/build/vite/dev.ts

test document/empty/undefined fetch destinations to ensure the
nitroDevMiddlewarePre fix is exercised (not just the catch-all)
@pi0 pi0 merged commit 3f16233 into main Mar 23, 2026
12 checks passed
@pi0 pi0 deleted the fix-vite-dotted branch March 23, 2026 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tanstack start + nitro (with basePath) infinite redirect for url parameter with dots

2 participants